package org.upc.edu.AIAExamples.communication;

import java.awt.*;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.renderer.xy.XYSplineRenderer;
import org.jfree.data.xy.*;
import org.jfree.ui.*;

public class Chart extends ApplicationFrame
{
	private static final long serialVersionUID = 1L;

	static class Panel extends JPanel
	{
		private static final long serialVersionUID = 1L;
		private XYDataset EAcumulada;
		private XYDataset Alpha;
		private XYDataset Diners;
		private XYDataset CCentral;

		private XYDataset SampleData(double[][] dades) {
			XYSeriesCollection xysc = null; // = new 
			for(int i=0; i<dades.length; i++) {
				XYSeries xyseries = new XYSeries("Casa "+i);
				for(int j=0; j<dades[i].length && dades[i][j]!=Double.NaN; j++) {
					xyseries.add(j,dades[i][j]);
				}
				if(xysc == null) xysc = new XYSeriesCollection(xyseries);
				else xysc.addSeries(xyseries);
			}
			return xysc;
		}
		
		private JTabbedPane createContent()
		{
			JTabbedPane jtabbedpane = new JTabbedPane();
			jtabbedpane.add("EAcumulada", createChartPanelEA());
			jtabbedpane.add("Alpha",createChartPanelA());
			jtabbedpane.add("Diners",createChartPanelD());
			jtabbedpane.add("C.Central",createChartPanelCC());
			return jtabbedpane;
		}

		private ChartPanel createChartPanelEA()
		{
			NumberAxis numberaxis = new NumberAxis("X");
			numberaxis.setAutoRangeIncludesZero(true);
			NumberAxis numberaxis1 = new NumberAxis("Y");
			numberaxis1.setAutoRangeIncludesZero(false);
			XYLineAndShapeRenderer xylineandshaperenderer = new XYLineAndShapeRenderer();
			XYPlot xyplot = new XYPlot(EAcumulada, numberaxis, numberaxis1, xylineandshaperenderer);
			xyplot.setBackgroundPaint(Color.lightGray);
			xyplot.setDomainGridlinePaint(Color.white);
			xyplot.setRangeGridlinePaint(Color.white);
			xyplot.setAxisOffset(new RectangleInsets(4D, 4D, 4D, 4D));
			JFreeChart jfreechart = new JFreeChart("Energia Acumulada", JFreeChart.DEFAULT_TITLE_FONT, xyplot, true);
			jfreechart.setBackgroundPaint(Color.white);
			ChartPanel chartpanel = new ChartPanel(jfreechart, false);
			return chartpanel;
		}
		
		private ChartPanel createChartPanelA()
		{
			NumberAxis numberaxis = new NumberAxis("X");
			numberaxis.setAutoRangeIncludesZero(true);
			NumberAxis numberaxis1 = new NumberAxis("Y");
			numberaxis1.setAutoRangeIncludesZero(false);
			XYLineAndShapeRenderer xylineandshaperenderer = new XYLineAndShapeRenderer();
			XYPlot xyplot = new XYPlot(Alpha, numberaxis, numberaxis1, xylineandshaperenderer);
			xyplot.setBackgroundPaint(Color.lightGray);
			xyplot.setDomainGridlinePaint(Color.white);
			xyplot.setRangeGridlinePaint(Color.white);
			xyplot.setAxisOffset(new RectangleInsets(4D, 4D, 4D, 4D));
			JFreeChart jfreechart = new JFreeChart("Alphes: Preu de reserva venedors", JFreeChart.DEFAULT_TITLE_FONT, xyplot, true);
			jfreechart.setBackgroundPaint(Color.white);
			ChartPanel chartpanel = new ChartPanel(jfreechart, false);
			return chartpanel;
		}
		
		private ChartPanel createChartPanelD()
		{
			NumberAxis numberaxis = new NumberAxis("X");
			numberaxis.setAutoRangeIncludesZero(true);
			NumberAxis numberaxis1 = new NumberAxis("Y");
			numberaxis1.setAutoRangeIncludesZero(false);
			XYLineAndShapeRenderer xylineandshaperenderer = new XYLineAndShapeRenderer();
			XYPlot xyplot = new XYPlot(Diners, numberaxis, numberaxis1, xylineandshaperenderer);
			xyplot.setBackgroundPaint(Color.lightGray);
			xyplot.setDomainGridlinePaint(Color.white);
			xyplot.setRangeGridlinePaint(Color.white);
			xyplot.setAxisOffset(new RectangleInsets(4D, 4D, 4D, 4D));
			JFreeChart jfreechart = new JFreeChart("Diners dels Agents", JFreeChart.DEFAULT_TITLE_FONT, xyplot, true);
			jfreechart.setBackgroundPaint(Color.white);
			ChartPanel chartpanel = new ChartPanel(jfreechart, false);
			return chartpanel;
		}
		
		private ChartPanel createChartPanelCC()
		{
			NumberAxis numberaxis = new NumberAxis("X");
			numberaxis.setAutoRangeIncludesZero(true);
			NumberAxis numberaxis1 = new NumberAxis("Y");
			numberaxis1.setAutoRangeIncludesZero(false);
			XYLineAndShapeRenderer xylineandshaperenderer = new XYLineAndShapeRenderer();
			XYPlot xyplot = new XYPlot(CCentral, numberaxis, numberaxis1, xylineandshaperenderer);
			xyplot.setBackgroundPaint(Color.lightGray);
			xyplot.setDomainGridlinePaint(Color.white);
			xyplot.setRangeGridlinePaint(Color.white);
			xyplot.setAxisOffset(new RectangleInsets(4D, 4D, 4D, 4D));
			JFreeChart jfreechart = new JFreeChart("Comandes a Central", JFreeChart.DEFAULT_TITLE_FONT, xyplot, true);
			jfreechart.setBackgroundPaint(Color.white);
			ChartPanel chartpanel = new ChartPanel(jfreechart, false);
			return chartpanel;
		}


		public Panel(double[][] dadesEA,double[][] dadesA, double[][] dadesD, double[][] dadesCC)
		{
			super(new BorderLayout());
			EAcumulada = SampleData(dadesEA);
			Alpha = SampleData(dadesA);
			Diners = SampleData(dadesD);
			CCentral = SampleData(dadesCC);
			add(createContent());
		}
	}


	public Chart(String s, double[][] dadesEA, double[][] dadesA, double[][] dadesD, double[][] dadesCC)
	{
		super(s);
		JPanel jpanel = new Panel(dadesEA,dadesA, dadesD, dadesCC);
		getContentPane().add(jpanel);
		this.pack();
		RefineryUtilities.centerFrameOnScreen(this);
		this.setVisible(true);
	}
}